<?php
header("Content-type: text/html; charset=UTF-8");

require_once('../settings.inc.php');
if(file_exists(X2_PROJECT ."/settings.inc.php")){
    require_once(X2_PROJECT ."/settings.inc.php");
}
require_once(X2. '/functions.inc.php');

$action = (isset($_REQUEST['action']))? $_REQUEST['action'] : 'view';
$js = "alert('При выполнении запроса произошла неопределенная ошибка')";

switch($action){
	case 'save':
        if(count($_POST) > 0 && isset($_POST['id'])){
	        include_once(X2 ."/classes/x2.class.php");
	        $x2 = x2::getInstance();
	        if($x2 -> user -> userInfo['create'] == 1 || $x2 -> user -> userInfo['update'] == 1){
                $x2 -> saveContent($_POST['id'], $_POST);
//              exit(var_export($x2));
                if($x2 -> db -> wasChanged() > 0){
          	        $js = "window.top.location.reload()";
                }
	        } else {
	            $js = "alert(\"Доступ запрещен\")";
          }
        }
    break;
    case 'set-state':
        if(count($_GET) > 0 && isset($_GET['id'])){
            include_once(X2 ."/classes/x2.class.php");
            $x2 = x2::getInstance();
            if($x2 -> user -> userInfo['create'] == 1 || $x2 -> user -> userInfo['update'] == 1){
                if($x2 -> setState($_GET['id'], $_GET['state'])){
                    $js = "window.top.location.reload()";
                }
	        } else {
	            $js = "alert(\"Доступ запрещен\")";
          }
        }
    break;
    default:
        $js = "";
}
?>
<html>
<body>
<?php
if($js != '') echo "<script type=\"text/javascript\">\n", $js, "\n</script>";
?>
</body>
</html>